#!/bin/bash

# 定义日志文件路径
LOG_FILE="/var/log/network_status.log"

# 检测网络接口状态的函数
check_interface_status() {
#    local interface=$1
    local interface=eth0
    local status=$(ip link show $interface | awk '/state UP/ {print "UP"}')

    if [[ "$status" == "UP" ]]; then
#        echo "$(date): Interface $interface is UP" >> $LOG_FILE
        echo "$(date): Interface $interface is UP" 
        echo "none" >/sys/class/leds/onecloud\:red\:alive/trigger 
        echo "none" >/sys/class/leds/onecloud\:blue\:alive/trigger
	echo "default-on" >/sys/class/leds/onecloud\:green\:alive/trigger 	
    else
#        echo "$(date): Interface $interface is DOWN" >> $LOG_FILE
        echo "$(date): Interface $interface is DOWN" 
        echo "none" >/sys/class/leds/onecloud\:green\:alive/trigger 
        echo "none" >/sys/class/leds/onecloud\:blue\:alive/trigger
	echo "default-on" >/sys/class/leds/onecloud\:red\:alive/trigger	
    fi
}

# 主循环
while true; do
    # 获取所有网络接口（排除 lo 等特殊接口）
    interfaces=$(ip link show | awk -F: '$0 !~ "lo|vir|wl|^[^0-9]"{print $2;getline}')

    for interface in $interfaces; do
        check_interface_status $interface
    done

    # 每隔一段时间检查一次（单位：秒）
    sleep 5
done